<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:20
 */

class CoinController extends AdminBaseController {

    /**
     * 商城购买报表
     */
    public function index(){
        $this->setTitle('货币产出与消耗');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $stateOptions = array('ALL' => '货币产出与消耗');

        $coinTypes = CoinService::getCoinType();
        $lod_special_id = $this->R('special_id');
        $lodos = $this->R('os');
        $filter->addSpecialFilter(false,2);
        $filter->addOsFilter($lod_special_id,true,2)
        ->addServerFilter($lod_special_id,$lodos,true,false,2)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2)
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2)
            //->addFilter('date', 'input', PubFunc::t('日期'), array('value' => date('Y-m-d',strtotime('yesterday')), 'date' => 'day'))
            ->addFilter('coinType', 'radio', PubFunc::t('货币'), array('options' => $coinTypes, 'value' => key($coinTypes)))
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'))
            ->addExport();

        $params = $filter->get();
        
        $params = $this->indexCommon($params);
        
        $header = CoinService::indexHeader($params);
        $data   = CoinService::indexData($params,array("CoinType"=>$coinTypes));

        if($filter->isExport()){
            $exportData     = array();
            $exportHeader   = CoinService::exportHeader();

            foreach($data['get'] as $v){
                $exportData[] = array(
                    'RecDate'   => $v['RecDate'],
                    'Type'      => '产出',
                    'Action'    => $v['Action'],
                    'Coin'      => $v['GetCoin'],
                    'User'      => $v['GetUser'],
                    'Cnt'       => $v['GetCnt'],
                    'Rate'      => $v['GetRate']
                );
            }
            foreach($data['used'] as $v){
                $exportData[] = array(
                    'RecDate'   => $v['RecDate'],
                    'Type'      => '消耗',
                    'Action'    => $v['Action'],
                    'Coin'      => $v['UsedCoin'],
                    'User'      => $v['UsedUser'],
                    'Cnt'       => $v['UsedCnt'],
                    'Rate'      => $v['UsedRate']
                );
            }

            $filename = LibExcel::getFilename('货币产出与消耗', $params);
            LibExcel::exportCsv($exportHeader, $exportData, $filename);
        }else{
            $chartConfig = CoinService::indexChart($data, $params);

            $chart->addChart('get_chart', $chartConfig['get']['data'], array(
                'name'   => PubFunc::t("货币产出表"),
                'type'   => 'bar',
                'config'=>array(
                    'xAxis' => array('axisLabel' => array('rotate' => 40)),
                    'normal'=>array(
                        'grid'=>array(
                            "x" => 100
                        ),
                    ),
                ),
            ));

            $chart->addChart('used_chart', $chartConfig['used']['data'], array(
                'name'   => PubFunc::t("货币消耗表"),
                'type'   => 'bar',
                'config'=>array(
                    'xAxis' => array('axisLabel' => array('rotate' => 40)),
                ),
            ));
            
            foreach($stateOptions as $k=>$v){
                if($k == $params['state']){
                    $grid->addGrid($header['get'], $data['get'], array(
                        'name'  => "货币产出表",
                        'order' => array('GetCoin', 'desc'),
                    ));
                    $grid->addGrid($header['used'], $data['used'], array(
                        'name'  => "货币消耗表",
                        'order' => array('UsedCoin', 'desc'),
                    ));
                }else{
                    $grid->addLink($k, $v);
                }
            }

            $chart->setShowType("float");
            $grid->setShowType("float");
            $this->out['filter'] = $filter->show();
            $this->out['chart'] = $chart->show();
            $this->out['grid'] = $grid->show();
            
            $this->out['allGetTotal'] = $data['allGetTotal'];
            $this->out['allUsedTotal'] = abs($data['allUsedTotal']);
            $this->out['residue'] = $data['residue'];
            $this->out['is_ana'] = 2;
        }
    }

    public function ranking(){
        $this->setTitle('统计富豪榜');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $coinTypes = CoinService::getCoinType();
        $filter
            ->addServerFilter()
            ->addFilter('date', 'input', '日期', array('value' => date('Y-m-d',strtotime('yesterday')), 'date' => 'day'))
            ->addFilter('coinType', 'radio', '货币', array('options' => $coinTypes, 'value' => key($coinTypes)))
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'))
            ->addExport();


        $params = $filter->get();
        $header = CoinService::rankingHeader();
        $data   = CoinService::rankingData($params);

        if($filter->isExport()){
            $exportData     = array();
            $exportHeader   = CoinService::exportRankingHeader();

            foreach($data['used'] as $v){
                $exportData[] = array(
                    'No'        => $v['No'],
                    'Type'      => '消费',
                    'RoleID'    => $v['RoleID'],
                    'RoleName'  => $v['RoleName'],
                    'GetNumber' => $v['GetNumber']
                );
            }

            foreach($data['get'] as $v){
                $exportData[] = array(
                    'No'        => $v['No'],
                    'Type'      => '获得',
                    'RoleID'    => $v['RoleID'],
                    'RoleName'  => $v['RoleName'],
                    'GetNumber' => $v['GetNumber']
                );
            }

            $filename = LibExcel::getFilename('统计富豪榜', $params);
            LibExcel::exportCsv($exportHeader, $exportData, $filename);
        }else {
            $grid->addGrid($header['used'], $data['used'], array(
                'name' => "货币消费排行",
                'order' => array('GetNumber', 'desc'),
                'showAll' => 1,
            ));
            $grid->addGrid($header['get'], $data['get'], array(
                'name' => "货币获得排行",
                'order' => array('GetNumber', 'desc'),
                'showAll' => 1,
            ));
            $grid->setShowType("float");
        }


        $this->out['filter'] = $filter->show();
        $this->out['grid'] = $grid->show();
    }


    public function cumRanking(){
        $this->setTitle('统计富豪榜');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();

        $coinTypes = CoinService::getCoinType();
        $filter
            ->addServerFilter()
            ->addFilter('date', 'input', '日期', array('value' => date('Y-m-d',strtotime('yesterday')), 'date' => 'day'))
            ->addFilter('coinType', 'radio', '货币', array('options' => $coinTypes, 'value' => key($coinTypes)))
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'))
            ->addExport();


        $params = $filter->get();
        $header = CoinService::rankingHeader();
        $data   = CoinService::cumRankingData($params);

        if($filter->isExport()){
            $exportData     = array();
            $exportHeader   = CoinService::exportRankingHeader();

            foreach($data['used'] as $v){
                $exportData[] = array(
                    'No'        => $v['No'],
                    'Type'      => '消费',
                    'RoleID'    => $v['RoleID'],
                    'RoleName'  => $v['RoleName'],
                    'GetNumber' => $v['GetNumber']
                );
            }

            foreach($data['get'] as $v){
                $exportData[] = array(
                    'No'        => $v['No'],
                    'Type'      => '获得',
                    'RoleID'    => $v['RoleID'],
                    'RoleName'  => $v['RoleName'],
                    'GetNumber' => $v['GetNumber']
                );
            }

            $filename = LibExcel::getFilename('统计富豪榜', $params);
            LibExcel::exportCsv($exportHeader, $exportData, $filename);
        }else {
            $grid->addGrid($header['used'], $data['used'], array(
                'name' => "货币消费排行",
                'order' => array('GetNumber', 'desc'),
                'showAll' => 1,
            ));
            $grid->addGrid($header['get'], $data['get'], array(
                'name' => "货币获得排行",
                'order' => array('GetNumber', 'desc'),
                'showAll' => 1,
            ));
            $grid->setShowType("float");
        }


        $this->out['filter'] = $filter->show();
        $this->out['grid'] = $grid->show();
    }

} 